package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    //根据手机号查询会员信息
<<<<<<< HEAD
=======
    @Override
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
<<<<<<< HEAD
=======
    @Override
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
    public void add(Member member) {
        //如果用户设置了密码，需要对密码进行md5加密
        String password = member.getPassword();
        if(password != null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        member.setRegTime(DateUtils.getToday());

        Date birthday = member.getBirthday();
        long time1 = new Date().getTime();
        long time2 = birthday.getTime();
        long age = (time1 - time2)/1000/60/60/24/365;
        member.setAge((int)age);

        memberDao.add(member);
    }

    //根据月份查询会员数量
<<<<<<< HEAD
=======
    @Override
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<>();
        if(months != null && months.size() > 0){
            for (String month : months) {//2019.01
                String endTime = month + ".31";
                Integer memberCount = memberDao.findMemberCountBeforeDate(endTime);
                memberCounts.add(memberCount);
            }
        }
        return memberCounts;
    }

    //分页查询
<<<<<<< HEAD
=======
    @Override
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Result delete(Integer id) {
        int count = orderDao.findCountByMemberId(id);
        if(count>0){
            return new Result(false, "删除会员失败，该会员已进行了预约");
        }
        memberDao.deleteById(id);
        return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
    }

    @Override
    public void edit(Member member) {
        Date birthday = member.getBirthday();
        long time1 = new Date().getTime();
        long time2 = birthday.getTime();
        long age = (time1 - time2)/1000/60/60/24/365;
        member.setAge((int)age);
        memberDao.edit(member);
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }
<<<<<<< HEAD

    @Override
    public Integer findMemberCountByAgeLimit(int low, int high) {
        System.out.println("low"+low);
        System.out.println("high"+high);
        Integer count =memberDao.findMemberCountByAgeLimit(low,high);
        System.out.println("count"+count);
        return memberDao.findMemberCountByAgeLimit(low,high);
    }

    @Override
    public List<Integer> getSexRatio() {
        List<Integer> list = new ArrayList<>();
        Integer maleCount =memberDao.findMaleCount();
        Integer femaleCount = memberDao.findFemaleCount();
        System.out.println("男"+maleCount+",女"+femaleCount);
        list.add(maleCount);
        list.add(femaleCount);
        return list;
    }
=======
>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
}
